<?php

class BelongsToAssociation extends Association
{

  protected $single = 1;

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IAssociationDefinition#getPrimaryKey()
   */
  public function getPrimaryKey()
  {
    if (empty($this->primaryKey)) {
      return $this->getSource()->getName() . '_' . $this->getForeignKey();
    }

    return parent::getPrimaryKey();
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/Association#loadData($client)
   */
  protected function loadData(IAggregate $client)
  {
    $def = $this->getDefinition();

    $dao = DAO::getInstance();

    $table = $def->getTarget()->getTable();
    $fk = $def->getForeignKey();
    $pk = $def->getPrimaryKey();

    $id = $client->get($pk);

    $result = $dao->query()->from($table)->where("$fk = ?")->execute($id);

    $obj = DomainMapper::inflateObj($def->getTarget(), $result->fetchRow());

    $meta = & $this->data($client);
    $meta['value'] = $obj;
  }

}

?>